package cn.class2.function

/**
  * Created by lzd on 2018/11/6.
  */
object WordCount {
  def main(args: Array[String]): Unit = {
    val arr = Array("hello tom hello lzd", "tom lzd hello dive", "hello world java dive")

    val res = arr.flatMap(_.split(" "))
    println(res.toBuffer)

    val res1 = res.map((_, 1))
    println(res1.toBuffer)

    val res2: Map[String, Array[(String, Int)]] = res1.groupBy(_._1)

    println(res2.toBuffer)

    var res3 = res2.map(t => (t._1, t._2.length))
    println(res3)
    res3 = res2.mapValues(_.length)
    println(res3)


    val res4 = res3.toList.sortBy(_._2).reverse
    println(res4)
  }
}
